package com.bookpavilion.article.service;

import com.bookpavilion.article.dao.CommentDao;
import com.bookpavilion.article.pojo.Article;
import com.bookpavilion.article.pojo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class CommentService {

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private IdWorker idWorker;

    public void addActicleComment(Comment comment){
        comment.set_id(idWorker.nextId()+"");
        comment.setPublish_date(new Date());
        commentDao.save(comment);
    }

    public Page<Comment> findCommentByParentId(String parentId, int page, int size){
        Pageable pageable = PageRequest.of(page-1,size);
        return commentDao.findByParentid(parentId,pageable);
    }

    public Page<Comment> findCommentByArticleId(String articleid,int page,int size){
        Pageable pageable=PageRequest.of(page-1,size);
        return commentDao.findByArticleid(articleid,pageable);
    }

    public void deleteArticleComment(String id){
        commentDao.deleteById(id);
    }

    public List<Comment> findAllArticleComment(){
        return commentDao.findAll();
    }

    public Comment findCommentById(String id) throws Exception{
        return commentDao.findById(id).get();
    }
}
